package com.wemei.auto.ui.helper

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.widget.Toast
import com.lxj.xpopup.XPopup
import com.wemei.auto.ui.ext.toastMessageLong


/**
 * 跳转地图导航
 */
object PopupNav {
    private const val PACKAGE_AMAP = "com.autonavi.minimap"
    private const val PACKAGE_AMAP_NAME = "高德地图"

    private const val PACKAGE_BAIDU = "com.baidu.BaiduMap"
    private const val PACKAGE_BAIDU_NAME = "百度地图"

    @JvmStatic
    fun goNav(context: Context, lat: Double, lon: Double, address: String) {
        val items = mutableListOf<String>()
        if (isNavigationApk(context, PACKAGE_AMAP)) {
            items.add(PACKAGE_AMAP_NAME)
        }
        if (isNavigationApk(context, PACKAGE_BAIDU)) {
            items.add(PACKAGE_BAIDU_NAME)
        }
        if (items.isEmpty()) {
            "未安装高德或者百度地图无法导航".toastMessageLong()
            return
        }
        if (items.size == 1) {
            nav(context, items[0], lat, lon, address)
        } else {
            XPopup.Builder(context)
                .asBottomList("请选择导航方式", items.toTypedArray()) { position, text ->
                    nav(context, text, lat, lon, address)
                }
                .show()
        }
    }

    @JvmStatic
    private fun nav(context: Context, textMap: String, lat: Double, lon: Double, address: String) {
        when (textMap) {
            PACKAGE_AMAP_NAME -> goGaoDeMap(context, lat, lon, address)
            PACKAGE_BAIDU_NAME -> goBaiduMap(context, lat, lon, address)
        }
    }

    /**
     * 高德地图
     * @param activity
     * @param lat 终点lat
     * @param lon 终点lon
     * @param address 终点地址名称
     */
    @JvmStatic
    fun goGaoDeMap(activity: Context, lat: Double, lon: Double, address: String) {
        if (isNavigationApk(activity, PACKAGE_AMAP)) {
            val uri: Uri =
                Uri.parse("amapuri://route/plan/?sourceApplication=${activity.packageName}&dlat=${lat}&dlon=${lon}&dname=${address}&dev=0&t=0")
            val intent = Intent(Intent.ACTION_VIEW, uri)
            intent.`package` = PACKAGE_AMAP
            activity.startActivity(intent)
        } else {
            Toast.makeText(activity, "您尚未安装高德地图", Toast.LENGTH_SHORT).show()
        }
    }

    /**
     * 跳转到百度地图
     *
     * @param activity
     * @param address  终点
     */
    @JvmStatic
    fun goBaiduMap(activity: Context, lat: Double, lon: Double, address: String) {
        if (isNavigationApk(activity, "com.baidu.BaiduMap")) {
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data =
                Uri.parse("baidumap://map/direction?destination=name:${address}|latlng:${lat},${lon}&coord_type=gcj02&mode=driving&src=${activity.packageName}")
            activity.startActivity(intent)
        } else {
            Toast.makeText(activity, "您尚未安装百度地图", Toast.LENGTH_SHORT).show()
        }
    }

    /**
     * 判断手机中是否有导航app
     *
     * @param activity
     * @param packagename 包名
     */
    @JvmStatic
    fun isNavigationApk(activity: Context, packagename: String): Boolean {
        val packages = activity.packageManager.getInstalledPackages(0)
        for (i in packages.indices) {
            val packageInfo = packages[i]
            return if (packageInfo.packageName == packagename) {
                true
            } else {
                continue
            }
        }
        return false
    }


}